<?
	error_reporting(E_ALL);
	define('IN_SITE', true);
	include ("common.php");
	
	$ses_pro = isset($_SESSION["ses_pro"]) ? $_SESSION["ses_pro"] : '' ;
	$ses_pros= isset($_SESSION["ses_pros"]) ? $_SESSION["ses_pros"] : '' ;
	
	$url=$_SERVER['HTTP_HOST'].$_SERVER['QUERY_STRING']; 
	
	include "headerjoin.php";
	//include "right.php";
	
	$template_body_page="template/user_reg.tpl";
	$template_comp_page="template/usercomm.tpl";
	$template_comp="template/userRegCom.tpl";
	$base_url 	= 'user.php';
 
	$url = substr($url,strpos($url,".php"),strlen($url));
	$url = explode("/",$url);
 
	
	$mode = isset($url[2]) ? $url[2] : '' ; 

	switch ($mode){
		case "doReg":		 
			cus_add();
			break ;
		case "docompReg":
			do_compReg();	 
			break;
		case "signout":
			do_signout();
			break;
		default:
			userReg();
	}
function userReg($mss=''){
	global $template, $db, $base_url,$template_body_page,$site_base_href;

	$ses_login=$_SESSION['ses_login']; 
	if($ses_login['s_user']){
		redirect($site_base_href."/user/index.php");
		exit();
	}
	 
	for($j=0;$j<5;$j++){
	 	$id_random .= chr(rand(65,90));
	}
	$sql="select catID,cat_Name from vb_ypcategory where cat_active=1 and cat_Level=1";

	if(!$result=$db->sql_query($sql)){
		message_die("Couldn't run the sql query!!!5", "", __LINE__, __FILE__, $sql);
	}
	
	$cat_count=$db->sql_numrows($result);
	$cat_data=$db->sql_fetchrowset($result);
	
	for($i=0;$i<$cat_count;$i++){
		$template->assign_block_vars("catrow", array(			
			'catValue'		=> $cat_data[$i]["catID"],	
			'catCaption'   	=> $cat_data[$i]["cat_Name"]		
		));
	}
	 
	$template->assign_vars(array(		
		'ranima' => base64_encode(base64_encode($id_random)),
		'uCheckID' =>$site_base_href.'/',
		'uCheckEmail' =>$site_base_href.'/',
		'getImage' =>$site_base_href.'/',
		's_action' =>$site_base_href.'/'.$base_url.'?/user/doReg',
		'url'		=> $site_base_href,
		'hdranima' =>$id_random,
		'r_mess' =>$mss
	)); 
	
	$template->set_filenames(array(	'body'	=> $template_body_page));
	$template->pparse('body');
}
//==================================================bo sung thong tin ca nhan======================================================
function cus_add(){ 
  global $db,$site_base_href;
 
  $txtID			= isset($_POST["txtID"]) ? htmlspecialchars($_POST["txtID"]) : '';
  $txtPassword		= isset($_POST["txtPassword"]) ? htmlspecialchars($_POST["txtPassword"]) : '';
  $txtEmail			= isset($_POST["txtEmail"]) ? htmlspecialchars($_POST["txtEmail"]) : '';
  $txtLastName		= isset($_POST["txtLastName"]) ? htmlspecialchars($_POST["txtLastName"]) : '';
  $txtFirstName		= isset($_POST["txtFirstName"]) ? htmlspecialchars($_POST["txtFirstName"]) : '';
  $optSex			= isset($_POST["optSex"]) ? htmlspecialchars($_POST["optSex"]) : '';
  $optIndustry		= isset($_POST["optIndustry"]) ? intval($_POST["optIndustry"]) : '';
  
  	for($j=0;$j<3;$j++){
	 	$VerificationCode .= chr(rand(65,90));
	  	$VerificationCode .= chr(rand(97,122));
	}
	$filecontent='Dear '.$txtFirstName.'&nbsp;'.$txtLastName.',<br> <br>';
	$filecontent.='Welcome to Yellow Page<br><br>User your Member ID and Password for all FREE services on Yellow Page.<br><br>';		
	$filecontent.='Member ID:'.$txtID."<br>";
	$filecontent.='Password :'.$txtPassword."<br><br>";
	$filecontent.='Upon initial Sign In , a Verification Code must be entered to validate your email address.This ensures accurate delivery of all Yellow Page correspondence and prevents fraud<br><br> '; 
	$filecontent.='Verification Code:'.$VerificationCode;
	$filecontent.='<br><br>For initial Sing in, Go to:<a href="'.$site_base_href.'/user/">Sign In '.'</a>';

	if(!empty($txtID) && !empty($txtEmail)){
	    $sql="select mLogin from vb_member where mLogin='".$txtID."' or mEmail='".$txtEmail."'";
		if(!$result=$db->sql_query($sql)){
			message_die("Couldn't run the sql query!!!1","",__LINE__,__FILE__,$sql);
		}
		$cat_count1=$db->sql_numrows($result);	
		$db->sql_freeresult($result);
		if($cat_count1>0){	//trùng thông tin đăng nhập đăng ký lại
		   userReg("This member ID is currently used. Please choose another one.");
		   return;
		}
		
		//thêm thành viên mới vào CSDL
		 $txtPassword = encrypt($txtPassword);
		 $sql="insert into vb_member(mLogin,mPassword,mEmail,mType,emailActive,datejoin,jointext) values('$txtID','$txtPassword','$txtEmail',$optIndustry,'$VerificationCode','".date("Y-m-d")."','".date("Y-m-d")."')"; 		
		if(!$result=$db->sql_query($sql)){
			message_die("Couldn't run the sql query!!!2", "", __LINE__, __FILE__, $sql);
		} 
	 
		$uid=$db->getRecord("select memID from vb_member where mEmail='$txtEmail'" );
		$sql="insert into vb_memberprofile(memID,FirstName,LastName,Sex,IndustryCategory) values($uid,'$txtFirstName','$txtLastName','$optSex',$optIndustry)";
		 
		if(!$result=$db->sql_query($sql)){
			message_die("Couldn't run the sql query!!!3", "", __LINE__, __FILE__, $sql);
		} 
		
		$sql="insert into vb_companyprofile(memID) values($uid)";
		if(!$result=$db->sql_query($sql)){
			message_die("Couldn't run the sql query!!!4", "", __LINE__, __FILE__, $sql);
		} 
	
		//Goi mail cho khach hang
		require_once("class/email.php");		
		$mail= new email();	
		$mail->to =$txtEmail;
		$mail->toname =$txtFirstName;
		$mail->fromname ="Yellowpages";
		$mail->from = "support@yellowpages.vn";
		$mail->enable_html = true;				
		$mail->subject = "Your Yellow Page Registration Confirmation";
		$mail->body = $filecontent;					
		$mail->send();		 
	
		//dang ky buoc 2
		compReg($uid);
	}else{
	
	}
}
function compReg($id=0){	
	// Hien thị trang đăng ký bước 2 : Thông tin công ty
	global $db,$template,$template_comp_page,$base_url,$site_base_href;
	
	$optSex = isset($_POST["optSex"]) ? htmlspecialchars($_POST["optSex"]) : '';
	  
	getBussType();
	getJobTitle();
	getContry();
	getState();
	getNumEmployee();
	
 	$txtID	= isset($_POST["txtID"]) ? htmlspecialchars($_POST["txtID"]) : '';
	$template->assign_vars(array(	
		'cusID' =>$id,
		'memID' =>$txtID,
		'url'	=> $site_base_href,
		's_action' =>$site_base_href.'/'.$base_url.'?/user/docompReg',
		'ucat'  =>$site_base_href.'/',
		'mr'	=> $optSex=='Male' ? "checked" : "",
		'ms'	=> $optSex=='Female' ? "checked" : "",
	)); 
	$template->set_filenames(array(
		'body' => $template_comp_page
	));
	$template->pparse('body');
}
function do_compReg(){
 
 global $db,$base_url,$site_base_href,$template,$template_comp;
 
  $hdID= isset($_POST["hdID"]) ? intval($_POST["hdID"]) : '';
   
 $txtCompany    = isset($_POST["txtCompany"]) ? htmlspecialchars($_POST["txtCompany"]) : '';
 $optBussinessType= isset($_POST["optBussinessType"]) ? intval($_POST["optBussinessType"]) : '';
 $txtAddress	= isset($_POST["txtAddress"]) ? htmlspecialchars($_POST["txtAddress"]) : '';
 $txtCity		= isset($_POST["txtCity"]) ? htmlspecialchars($_POST["txtCity"]) : '';
 $txtZipCode	= isset($_POST["txtZipCode"]) ? htmlspecialchars($_POST["txtZipCode"]) : '';
 $txtState		= isset($_POST["txtState"]) ? htmlspecialchars($_POST["txtState"]) : '';
 
 $optJobTitle		= isset($_POST["optJobTitle"]) ? intval($_POST["optJobTitle"]) :0;
 $txtContrycode		= isset($_POST["txtContrycode"]) ? htmlspecialchars($_POST["txtContrycode"]) : '';
 $txtAreacode		= isset($_POST["txtAreacode"]) ? htmlspecialchars($_POST["txtAreacode"]) : '';
 $txtBussinessPhone	= isset($_POST["txtBussinessPhone"]) ? htmlspecialchars($_POST["txtBussinessPhone"]) : '';
 $txtContrycodefax	= isset($_POST["txtContrycodefax"]) ? htmlspecialchars($_POST["txtContrycodefax"]) : '';
 $txtAreacodefax	= isset($_POST["txtAreacodefax"]) ? htmlspecialchars($_POST["txtAreacodefax"]) : '';
 $txtBussinessPhonefax= isset($_POST["txtBussinessPhonefax"]) ? htmlspecialchars($_POST["txtBussinessPhonefax"]) : '';
 $txtMobile			= isset($_POST["txtMobile"]) ? htmlspecialchars($_POST["txtMobile"]) : '';
 $txtKeyProduct1	= isset($_POST["txtKeyProduct1"]) ? htmlspecialchars($_POST["txtKeyProduct1"]) : '';
 $txtKeyProduct2	= isset($_POST["txtKeyProduct2"]) ? htmlspecialchars($_POST["txtKeyProduct2"]) : '';
 $txtKeyProduct3	= isset($_POST["txtKeyProduct3"]) ? htmlspecialchars($_POST["txtKeyProduct3"]) : '';
 
 $optCategory	= isset($_POST["hdcategory"]) ? htmlspecialchars($_POST["hdcategory"]) : '';
 
 $catSearch=ereg_replace("-",",",$optCategory);

 $catSearch=repStr($catSearch);
  
 $txtKeyProduct		= $txtKeyProduct1.",".$txtKeyProduct2.",".$txtKeyProduct3;
 $optMainMarkets	= isset($_POST["optMainMarkets"]) ? intval($_POST["optMainMarkets"]) : 0;
 $txtOrtherMarket	= isset($_POST["txtOrtherMarket"]) ? htmlspecialchars($_POST["txtOrtherMarket"]) : '';
 $optNumberEmploy	= isset($_POST["optNumberEmploy"]) ? intval($_POST["optNumberEmploy"]) : 0;
 $txtCEO			= isset($_POST["txtCEO"]) ? htmlspecialchars($_POST["txtCEO"]) : '';
 $txtYearEstab		= isset($_POST["txtYearEstab"]) ? htmlspecialchars($_POST["txtYearEstab"]) : '';
 $txtCertifi		= isset($_POST["txtCertifi"]) ? htmlspecialchars($_POST["txtCertifi"]) : '';
 $txtInfo			= isset($_POST["txtInfo"]) ? htmlspecialchars($_POST["txtInfo"]) : '';
 $optContry			= isset($_POST["optContry"]) ? intval($_POST["optContry"]) :0; 
 
 $txtFirstName		= isset($_POST["txtFirstName"]) ? htmlspecialchars($_POST["txtFirstName"]) : '';
 $txtLastName		= isset($_POST["txtLastName"]) ? htmlspecialchars($_POST["txtLastName"]) : '';
 $txtWebSite		= isset($_POST["txtWebSite"]) ? htmlspecialchars($_POST["txtWebSite"]) : '';
 $txtBusinessEmail	= isset($_POST["txtBusinessEmail"]) ? htmlspecialchars($_POST["txtBusinessEmail"]) : '';
 	if(empty($_POST)) {
		redirect("http://localhost/myonline/index.php?flag=1");
		return ;
	}
 
  	$sql="update vb_memberprofile set CompanyName='$txtCompany',BusinessType=$optBussinessType,Address='$txtAddress', City='$txtCity',Zip='$txtZipCode',State='$txtState',JobTitle=$optJobTitle,  contrycode='$txtContrycode', areacode='$txtAreacode', BusinessPhone='$txtBussinessPhone',faxcontrycode='$txtContrycodefax',AlternativeEmail ='$txtBusinessEmail', faxareacode ='$txtAreacodefax', FaxNumber='$txtBussinessPhonefax',Mobile='$txtMobile', countryID= $optContry,fName='$txtFirstName', lName='$txtLastName',Web='$txtWebSite' where memID=".$hdID; 
 
	if(!$result=$db->sql_query($sql)){
		message_die("Couldn't run the sql query !", "", __LINE__, __FILE__, $sql);
	}
	
	$sql="update vb_companyprofile set Category='$optCategory',KeyProSer='$txtKeyProduct',Legal='$txtCEO',YearEstablished='$txtYearEstab',Certificate='$txtCertifi',Market=$optMainMarkets,CompanyIntroduction='$txtInfo',NumberEmployees=$optNumberEmploy,catSearch='$catSearch'  where memID=".$hdID;
  
  if(!$result=$db->sql_query($sql)){
		message_die("Couldn't run the sql query !!", "", __LINE__, __FILE__, $sql);
	}
	
  $template->assign_vars(array(			 
		'url'		=> $site_base_href,	
	)); 
 $template->set_filenames(array(
		'body'		=> $template_comp
	));
	$template->pparse('body');
	return;
	// redirect($site_base_href.'/userLog.php');
	
}
function do_signout(){
	global $db, $site_option, $base_url,$site_base_href;	 
	$_SESSION['ses_login']	= '';
	session_unregister("ses_login");
	//redirect($site_base_href."/userLog.php?/user/signout");
	redirect("http://localhost/myonline/index.php?flag=1");
}

	include "bottom3-5.php";
	 $db->sql_close();
?>